package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {


        List<Strategy> list = strategyService.list();

        StrategyStatisVO vo = null;

        for (Strategy strategy : list) {

            if (strategyStatisVORedisService.isEisit(strategy)){
                continue;
            }
            vo = new StrategyStatisVO();

            BeanUtils.copyProperties(strategy, vo);

            vo.setStrategyId(strategy.getId());

            strategyStatisVORedisService.setStrategyStatisVO(vo);
        }
    }
}
